package com.teach;

import java.io.File;
import java.util.Arrays;

/**
 * <pre>
 * -
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-04-27 14:10
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
public class HomeWork01 {

  public static void main(String[] args) {
    printTree(new File("d:/upload/"),"");
  }

  public static void printTree1(File dir,String prv) {
    for(File f : dir.listFiles(item->item.isDirectory())){
      System.out.println(prv+"□ "+f.getName());
//      printTree(f,prv+"  ");
    }
    for(File f : dir.listFiles(item->item.isFile())){
      System.out.println(prv+"■ "+f.getName());
    }
  }

  public static void printTree(File dir,String prv) {
    File[] files = dir.listFiles();
    Arrays.sort(files,(a,b)->{
      if(a.isFile()&&b.isFile()){
        return a.compareTo(b);
      }else if(a.isDirectory()&&b.isDirectory()){
        return a.compareTo(b);
      }else if(a.isDirectory()){
        return -1;
      }else{
        return 1;
      }
    });
    for(int i=0;i<files.length;i++){
      System.out.print(prv);
      if(i==files.length-1){
        System.out.print("└");
      }else {
        System.out.print("├");
      }
      System.out.print(files[i].isFile()?"■ ":"□ ");
      System.out.println(files[i].getName());
      if(files[i].isDirectory()){
        if(i==files.length-1){
          printTree(files[i],prv+" ");
        }else{
          printTree(files[i],prv+"│");
        }

      }
    }
  }
}
